<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader @enterList="enterHandler" :list="list"></TodoHeader>
    <TodoMain :list="isShow" @delList="delHandler"></TodoMain>
    <TodoFooter
      :list="isShow"
      @isSelInfo="isSelHandler"
      @clearList="clearHandler"
    ></TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
import "./styles/base.css";
import "./styles/index.css";

import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodoMain.vue";
import TodoFooter from "./components/TodoFooter.vue";

export default {
  data() {
    return {
      list: JSON.parse(localStorage.getItem("list") || "[]"),
      getSel: "all",
    };
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  methods: {
    enterHandler(taskName) {
      let id =
        this.list.length > 0 ? this.list[this.list.length - 1].id + 1 : 100;
      this.list.push({
        id,
        name: taskName,
        isDone: false,
      });
    },
    delHandler(id) {
      let index = this.list.findIndex((item) => item.id === id);
      this.list.splice(index, 1);
    },
    isSelHandler(str) {
      this.getSel = str;
    },
    clearHandler() {
      this.list = this.list.filter((item) => !item.isDone);
    },
  },
  computed: {
    isShow() {
      if (this.getSel == "no") {
        return this.list.filter((item) => !item.isDone);
      } else if (this.getSel == "yes") {
        return this.list.filter((item) => item.isDone);
      } else {
        return this.list;
      }
    },
  },
  watch: {
    list: {
      deep: true,
      handler() {
        localStorage.setItem("list", JSON.stringify(this.list));
      },
    },
  },
};
</script>